<?php

namespace app\admin\controller\shopro\activity;

use addons\shopro\model\RobOrderModel;
use app\admin\controller\shopro\Base;

/**
 * 预约列表
 * Class RobOrder
 * @package app\admin\controller\coach
 */
class RobOrder extends Base
{

    /**
     * RobOrderModel模型对象
     * @var \addons\shopro\model\RobOrderModel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new RobOrderModel();

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 获取列表
     * @return mixed|string|\think\response\Json
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if (request()->isAjax()) {
            $page      = input('param.page/d', 1);
            $rows      = input('param.rows/d', 20);
            $key       = input('param.keyword', '');
            $type      = input('param.type', '');
            $aid       = input('param.aid', '');
            $isRob     = input('param.is_rob', '');
            $startTime = input('param.start_time', '');
            $endTime   = input('param.end_time', '');
            $result    = $this->model->getLists([
                'keyword'     => $key,
                'type'        => $type,
                'activity_id' => $aid,
                'is_rob'      => $isRob,
                'start_time'  => $startTime,
                'end_time'    => $endTime
            ], $rows, $page, [
                'a.id',
                'a.title',
                'a.activity_price',
                'a.is_use',
                'a.is_rob',
                'a.create_time',
                'b.nickname',
                'b.avatar',
                'b.mobile',
                'c.title' => 'goods_title',
                'c.image'
            ]);

            $result['activityList'] = \addons\shopro\model\Activity::where(['type' => 'robbuy'])
                ->field(['id', 'title', 'starttime', 'endtime'])
                ->select();

            return json($result);
        }

        return $this->fetch();
    }

    // 添加/编辑
    public function add()
    {
        $data = input('post.');
        if (!empty($data)) {
            // 编辑
            $data['update_time'] = time();

            if ($this->model->edit($data)) {
                return json([
                    'code' => 200,
                    'msg'  => '编辑成功'
                ]);
            } else {
                return json([
                    'code' => 400,
                    'msg'  => '编辑失败'
                ]);
            }
        }
    }

    /**
     * 更改抢购名额
     * @return \think\response\Json
     */
    public function status()
    {
        $data  = input('param.');
        $order = $this->model->getItem($data['id']);

        if ($order->is_use == 1) {
            return json([
                'code' => 401,
                'msg'  => '已使用过的名额无法更改'
            ]);
        }

        if ($data['status'] == 1) {
            // 查看名额是否还有
            $num   = \addons\shopro\model\Activity::where(['id' => $order->activity_id])
                ->value('num');
            $count = $this->model->where(['is_rob' => 1])->count();

            if ($count == $num) {
                return json([
                    'code' => 402,
                    'msg'  => '抢购名额已满'
                ]);
            }
        }

        $editData = [
            'id'     => $data['id'],
            'is_rob' => $data['status']
        ];
        if ($this->model->edit($editData)) {
            return json([
                'code' => 200,
                'msg'  => '更改成功'
            ]);
        } else {
            return json([
                'code' => 400,
                'msg'  => '更改失败'
            ]);
        }
    }

    // 删除
    public function dell()
    {
        $id = input('id');
        if (!empty($id)) {
            $ids = strpos($id, ',') !== false ? explode(',', $id) : [$id];
            foreach ($ids as $v) {
                if (!$this->model->getItem((int)$v)) {
                    return json(['code' => 401, 'msg' => ' 您要删除的预约不存在']);
                }
            }
            foreach ($ids as $v) {
                $this->model->del(['id' => $v]);
            }
            return json(['code' => 200, 'msg' => '删除成功']);
        }
    }

}
